package com.yanxu;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-05-29-2025
 * @Description: 客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = null;
        try {
            // 创建 socket , 连接服务端 9999 端口
            socket = new Socket("127.0.0.1", 9999);
            //socket.connect(new InetSocketAddress("127.0.0.1",9999));

            // 获取 reader、 writer
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            // 获取键盘输入 reader。 用于接收键盘的输入信息
            BufferedReader consoleReader = new BufferedReader(
                    new InputStreamReader(System.in)
            );

            // 死循环，不断的读取键盘的输入
            while (true) {
                String data = consoleReader.readLine();

                // 将键盘输入的内容发送到 服务端
                writer.write(data + "\n");
                writer.flush();

                // 读取服务端发送过来的数据
                String msg = reader.readLine();
                System.out.println(msg);

                // 当控制台输入的是 “bey”, 代表 客户端想要退出了。
                if (msg.equals("bye")) {
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }


    }
}